//
//  YHYNetworkManager+Extension.swift
//  微博(学习)
//
//  Created by yhy on 16/11/16.
//  Copyright © 2016年 yhy. All rights reserved.
//

import Foundation

// MARK: - 封装新浪微博的网络请求方法
extension YHYNetworkManager {
    
    /// 加载微博数据字典数组
    ///
    /// - parameter complrtion: 完成回调[list: 微博字典数组/是否成功]
    /// - parameter since_id:   返回ID比since_id大的微博 (即比since_id时间晚的微博), 默认为0
    /// - parameter max_id:     返回ID小于或等于max_id的微博, 默认为0
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping (_ list: [[String: AnyObject]]?, _ isSuccess: Bool)->()) {
        
        
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        // Swift 中 Int 可以转换成 AnyObject/但是 Int64 不行
        let params = ["since_id": "\(since_id)", "max_id": "\(max_id > 0 ? max_id - 1 : 0)"]
        
       
        
        tokenRequest(URLString: url, parameters: params as [String : AnyObject]?) { (json,isSuccess) in
            
//            if let json = json {
//                print(json)
//                
//            }
            
            
            // 从 json 中获取 statuses 字典数组
            // 如果 as? 失败, result = nil
            // 服务器返回的字典数组,就是按照时间的倒序排序的
            let result = json?["statuses"] as? [[String: AnyObject]]
            
//            print(result)
            
            completion(result, true)
            
            
        }
        
    }
    
    /// 返回微博的未读数量 - 定时刷新, 不需要提示失败
    func unreadCount(completion:@escaping (_ count: Int)->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["udi": uid]
        
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            //print(json)
            
            let dic = json as? [String: AnyObject]
            let count = dic?["status"] as? Int
            completion(count ?? 0)
            
            
            
        }
    }
    
}

// MARK: - OAuth 相关方法
extension YHYNetworkManager {
    
    /// 提问: 异步应该返回什么? - 需要什么返回什么
    
    /// 加载 accessToken
    /// 
    /// - parameter code:       授权码
    /// - parameter completion: 完成回调(是否成功)
    func loadAccessToken(code: String, completion: @escaping (_ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":WBAppKey,
                      "client_secret":WBAppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WBRedirectURL]
        
        //发起网络请求
        request(method: .POST, URLString: urlString, parameters: params as [String : AnyObject]?, completion: {(json, isSuccess)->() in
            
            print(json)
            
            // 如果请求失败, 对用户账户数据没有任何影响
            // 直接用字典设置 userAccount 的属性
            self.userAccount.yy_modelSet(with: (json as? [AnyHashable : Any]) ?? [:])
            
            print(self.userAccount)
            
            // 保存模型
            
            self.userAccount.saveAccount()
            
            // 完成回调
            completion(true)
            
        })
        
    }
}




